/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Plugin;
import me.zhengjie.modules.system.domain.vo.PluginCreateVo;
import me.zhengjie.modules.system.domain.vo.PluginQueryCriteria;
import me.zhengjie.modules.system.service.PluginService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author lxp
* @date 2024-11-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "PluginController管理")
@RequestMapping("/api/plugin")
public class PluginController {

    private final PluginService pluginService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('plugin:list')")
    public void exportPlugin(HttpServletResponse response, PluginQueryCriteria criteria) throws IOException {
        pluginService.download(pluginService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("插件分页查询")
    @ApiOperation("插件分页查询")
    //@PreAuthorize("@el.check('plugin:list')")
    public ResponseEntity<PageResult<Plugin>> queryPlugin(PluginQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(pluginService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/detail/{id}")
    @Log("插件详情")
    @ApiOperation("插件详情")
    //@PreAuthorize("@el.check('plugin:list')")
    public ResponseEntity<Plugin> queryPlugin(@PathVariable Long id){
        return new ResponseEntity<>(pluginService.getPluginById(id),HttpStatus.OK);
    }

    @PostMapping(consumes = "multipart/form-data")
    @Log("新增插件")
    @ApiOperation("新增插件")
    //@PreAuthorize("@el.check('plugin:add')")
    public ResponseEntity<Object> createPlugin(@Validated @ModelAttribute PluginCreateVo resources){
        pluginService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping(consumes = "multipart/form-data")
    @Log("修改插件")
    @ApiOperation("修改插件")
    //@PreAuthorize("@el.check('plugin:edit')")
    public ResponseEntity<Boolean> updatePlugin(@Validated @ModelAttribute PluginCreateVo resources){
        pluginService.update(resources);
        return new ResponseEntity<>(true,HttpStatus.OK);
    }

//    @DeleteMapping
//    @Log("删除PluginController")
//    @ApiOperation("删除PluginController")
//    //@PreAuthorize("@el.check('plugin:del')")
//    public ResponseEntity<Object> deletePlugin(@RequestBody List<Long> ids) {
//        pluginService.deleteAll(ids);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
}